1Writer リピートタスク 一括コピー4
2024/11/04
以下に今回対応したリピートタスクの仕様をまとめます。
リピートタスク仕様
1. n日おき
• 記述形式:@repeat <n>日毎 <基準日>
• 説明:指定した基準日から、n日 + 1の間隔でリピートする。
• 例:@repeat 3日毎 2024-10-29の場合、2024年10月29日を基準に3日おきにタスクが繰り返されます。
2. 特定の曜日
• 記述形式:@repeat <曜日><曜日>...
• 説明:指定した曜日にリピートします。曜日は日本語で「日、月、火、水、木、金、土」として記述。
• 例:@repeat 月, 水, 金の場合、毎週月曜日、水曜日、金曜日にタスクがリピートされます。
3. 平日
• 記述形式:@repeat 平日
• 説明:毎週月曜日から金曜日の間でタスクがリピートされます。
• 例:@repeat 平日と記述すると、月曜日から金曜日にリピートタスクが生成されます。
4. 毎月特定の日
• 記述形式:@repeat 月次 <日>
• 説明:毎月の指定された日にリピートします。
• 例:@repeat 月次 15と記述すると、毎月15日にタスクがリピートされます。
5. 毎年特定の日付
• 記述形式:@repeat 年次 <月>-<日>
• 説明:毎年指定された月日のタイミングでリピートします。
• 例:@repeat 年次 12-25と記述すると、毎年12月25日にタスクがリピートされます。
使用例
• タスク1 @repeat 3日毎 2024-10-29
• リピート内容:2024年10月29日を基準に3日おきにリピート。
• タスク2 @repeat 月, 水, 金
• リピート内容:毎週月曜日、水曜日、金曜日にリピート。
• タスク3 @repeat 平日
• リピート内容:毎週月曜日から金曜日にリピート。
• タスク4 @repeat 月次 15
• リピート内容:毎月15日にリピート。
• タスク5 @repeat 年次 12-25
• リピート内容:毎年12月25日にリピート。
この仕様に基づいて、各タスクのリピート日を判定して、対応するデイリーノートの日付にリピートタスクを追加します。
code:js
function generateRepeatTasks() {
const filePath = editor.getFolderPath() + "/リピートタスク.md";
// 現在開いているデイリーノートの日付を取得
const dailyNoteDate = getDailyNoteDate();
if (!dailyNoteDate) {
ui.hudError("日付形式が無効です。デイリーノートのファイル名を確認してください。");
return;
}
// 現在開いているデイリーノートのカーソル位置を取得
const cursorPosition = editor.getSelectedRange()0; // リピートタスクファイルを開き、成功後にgetTextで内容を取得
editor.openFile(filePath, 'edit', () => {
const content = editor.getText();
if (!content) {
ui.hudError("リピートタスク.mdが読み込めませんでした。ファイルが存在するか確認してください。");
return;
}
const tasks = content.split('\n');
const newTasks = [];
tasks.forEach(task => {
const repeatMatch = task.match(/@repeat (.+)/);
if (!repeatMatch) {
// @repeat指定がないタスクはそのままコピーし、末尾に⟳を追加
const taskText = task.trim();
if (taskText.startsWith("###")) {
// セクション見出しの場合はそのまま
newTasks.push(taskText);
} else {
// 通常のタスクには⟳を追加
newTasks.push(${taskText} ⟳);
}
return;
}
const repeatRule = repeatMatch1; const isRepeatDay = checkRepeatDay(dailyNoteDate, repeatRule);
// デイリーノートの日付がリピート日に該当する場合のみコピー
if (isRepeatDay) {
const taskText = task.replace(/@repeat .+/, '').trim();
if (taskText.startsWith("###")) {
// セクション見出しには⟳を付けずにそのまま追加
newTasks.push(taskText);
} else {
// 通常のタスクには⟳を追加
newTasks.push(${taskText} ⟳);
}
}
});
if (newTasks.length > 0) {
const newFilePath = editor.getFolderPath() + /${dailyNoteDate}.md;
// カーソル位置を取得し、そこに新しいタスクを挿入
editor.openFile(newFilePath, 'edit', () => {
const tasksToInsert = newTasks.join('\n') + '\n';
editor.replaceTextInRange(cursorPosition, cursorPosition, tasksToInsert);
ui.hudSuccess("次回のリピートタスクを生成しました!");
});
} else {
ui.hudError("一致するタスクがありません。");
}
});
}
// 開いているデイリーノートの日付を取得(ファイル名が "YYYY-MM-DD.md" の形式であると仮定)
function getDailyNoteDate() {
const fileName = editor.getFileName();
const dateMatch = fileName.match(/^(\d{4}-\d{2}-\d{2})/);
return dateMatch ? dateMatch1 : null; }
// 各リピートルールに対応した判定関数
function checkRepeatDay(noteDate, rule) {
const noteDateObj = new Date(noteDate);
const dayIntervalMatch = rule.match(/^(\d+)日毎(?: (\d{4}-\d{2}-\d{2}))?$/);
const weekdaysMatch = rule.match(/^(平日)$/);
const specificDaysMatch = rule.match(/^(日月火水木金土,?)+$/); const monthlyMatch = rule.match(/^月次 (\d{1,2})$/);
const yearlyMatch = rule.match(/^年次 (\d{1,2})-(\d{1,2})$/);
if (dayIntervalMatch) {
// n日毎の判定
const nDays = parseInt(dayIntervalMatch1, 10); const baseDate = dayIntervalMatch2 ? new Date(dayIntervalMatch2) : noteDateObj; const diffDays = Math.floor((noteDateObj - baseDate) / (1000 * 60 * 60 * 24));
return diffDays >= 0 && (diffDays % (nDays + 1)) === 0;
} else if (weekdaysMatch) {
// 平日判定(月~金)
const dayOfWeek = noteDateObj.getDay();
return dayOfWeek >= 1 && dayOfWeek <= 5;
} else if (specificDaysMatch) {
// 特定の曜日(例: 月, 水, 金)
const days = rule.split(',').map(day => day.trim());
return days.includes(dayOfWeek);
} else if (monthlyMatch) {
// 毎月特定の日
const dayOfMonth = parseInt(monthlyMatch1, 10); return noteDateObj.getDate() === dayOfMonth;
} else if (yearlyMatch) {
// 毎年特定の日付
const month = parseInt(yearlyMatch1, 10) - 1; const day = parseInt(yearlyMatch2, 10); return noteDateObj.getMonth() === month && noteDateObj.getDate() === day;
}
return false;
}
// 日付を "YYYY-MM-DD" フォーマットで文字列に変換
function formatDate(date) {
const yyyy = date.getFullYear();
const mm = String(date.getMonth() + 1).padStart(2, '0');
const dd = String(date.getDate()).padStart(2, '0');
return ${yyyy}-${mm}-${dd};
}
generateRepeatTasks();